package com.yami.shop.api.controller;

import com.yami.shop.api.config.DateUtil;
import com.yami.shop.api.config.HttpClient;
import com.yami.shop.api.config.ParamBuilder;
import com.yami.shop.api.config.SignHelper;
import com.yami.shop.bean.meituan.CreateShop;
import com.yami.shop.bean.meituan.OpenApiConfig;
import com.yami.shop.bean.meituan.OrderStatusInfo;
import com.yami.shop.bean.meituan.RequestConstant;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.UserAddr;
import com.yami.shop.bean.model.UserAddrOrder;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.OrderStatusInfoService;
import com.yami.shop.service.UserAddrOrderService;
import com.yami.shop.service.UserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/QueryOrderStatusInfo")
public class QueryOrderStatusInfoController {

    @Autowired
    private OrderStatusInfoService orderStatusInfoService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserAddrService userAddrService;

    @Autowired
    private UserAddrOrderService userAddrOrderService;


    @PostMapping("/getOrderStatusInfo")
    public  void getOrderStatusInfo() throws Exception {
        String appkey = OpenApiConfig.FORMAL_APP_KEY;
        String secret = OpenApiConfig.FORMAL_SECRET;
        String res=null;
        OrderStatusInfo request = buildMockRequest(appkey);

        Map<String, String> params = ParamBuilder.convertToMap(request);
        String sign = SignHelper.generateSign(params, secret);
        params.put("sign", sign);
        params.put("appkey",appkey);
        params.put("timestamp",String.valueOf(DateUtil.unixTime()));
        params.put("version","1.0");
        try {
            res = HttpClient.post(RequestConstant.ORDER_QUERY, params);
        } catch (Exception e) {
            throw e;
        }
    }

    private OrderStatusInfo buildMockRequest(String appkey) {
        OrderStatusInfo request = new OrderStatusInfo();
        request.setDeliveryId(String.valueOf(1593831798L));
        request.setMtPeisongId("1593831848039001677");
        return request;
    }

    @GetMapping("/getRiderLocation")
    public ResponseEntity<Map> getRiderLocation(String orderNumber) throws  Exception{
        Map map = new HashMap();
        String res = null;
        String appkey = OpenApiConfig.FORMAL_APP_KEY;
        String secret = OpenApiConfig.FORMAL_SECRET;
        OrderStatusInfo orderStatusInfo =  orderStatusInfoService.getPeiSongId(orderNumber);
        OrderStatusInfo request = buildLcoationRequest(orderNumber,orderStatusInfo);
        Map<String, String> params = ParamBuilder.convertToMap(request);
        String sign = SignHelper.generateSign(params, secret);
        params.put("sign", sign);
        params.put("appkey",appkey);
        params.put("timestamp",String.valueOf(DateUtil.unixTime()));
        params.put("version","1.0");
        try {
            res = HttpClient.post(RequestConstant.GET_RIDER_LOCATION , params);
//            Map testMap = new HashMap();
//            Map newMap = new HashMap();
//            newMap.put("lat","32121587");
//            newMap.put("lng","112190580");
//            testMap.put("code","0");
//            testMap.put("data",newMap);
            map.put("riderLocation",res);
            //获取订单配送状态
            String deliveryStatus = orderStatusInfo.getStatus();
            map.put("deliveryStatus",deliveryStatus);
            //获取当前收货人的经纬度
            Order order  = orderService.getOrderByOrderNumber(orderNumber);
            Long addorderId = order.getAddrOrderId();
            UserAddrOrder userAddrOrder = userAddrOrderService.getById(addorderId);
            if(userAddrOrder!=null) {
                Long AddrId = userAddrOrder.getAddrId();
                UserAddr userAddr = userAddrService.getById(AddrId);
                String lat = userAddr.getLat();
                String lng = userAddr.getLng();
                map.put("userLat",lat);
                map.put("userLng",lng);
            }
        } catch (Exception e) {
            throw e;
        }
        return ResponseEntity.ok(map);
    }

    private OrderStatusInfo buildLcoationRequest(String orderNumber,OrderStatusInfo orderStatusInfo ) {
        OrderStatusInfo request = new OrderStatusInfo();
        request.setDeliveryId(orderNumber);
        request.setMtPeisongId(orderStatusInfo.getMtPeisongId());
        return request;
    }


}
